package org.ihelpuoo.db.sql;

import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Iterator;

import org.ihelpuoo.lang.LoaderException;
import org.ihelpuoo.lang.RenderException;
import org.ihelpuoo.parser.AbstractTextEngine;
import org.ihelpuoo.parser.segment.CharSegment;
import org.ihelpuoo.parser.segment.Segment;
import org.ihelpuoo.parser.segment.SegmentUtils;

/**
 * <b>SqlSegment class:</b>
 * <hr color=grey size=1>
 *
 *
 *
 * @author zozoh  created @ 2006
 *
 */
public class SQLSegment extends AbstractTextEngine implements Segment
{

	private CharSegment	seg;

	public SQLSegment()
	{
		seg = new CharSegment();
	}

	public SQLSegment(Reader r) throws LoaderException
	{
		seg = new CharSegment(r);
	}

	public SQLSegment(InputStream ins) throws LoaderException
	{
		seg = new CharSegment(ins);
	}

	public SQLSegment(CharSequence cs) throws LoaderException
	{
		seg = new CharSegment(cs);
	}

	public Segment set(String key, boolean v)
	{
		return seg.set(key,v);
	}

	public Segment set(String key, int v)
	{
		return seg.set(key,v);
	}

	public Segment set(String key, byte v)
	{
		return seg.set(key,v);
	}

	public Segment set(String key, double v)
	{
		return seg.set(key,v);
	}

	public Segment set(String key, float v)
	{
		return seg.set(key,v);
	}

	public Segment set(String key, long v)
	{
		return seg.set(key,v);
	}

	public Segment set(String key, short v)
	{
		return seg.set(key,v);
	}

	public Segment set(String key, Object v)
	{
		if (null == v)
			return seg.set(key,v);
		return seg.set(key,SQLUtils.escapeFieldValue(v.toString()));
	}

	public Segment add(String key, Object v)
	{
		return seg.add(key,v);
	}

	public Segment setUnescape(String key, Object v)
	{
		return seg.set(key,v);
	}

	public void clearAll()
	{
		seg.clearAll();
	}

	public Iterator<String> iterator()
	{
		return seg.iterator();
	}

	public int size()
	{
		return seg.size();
	}

	public int itemNumber()
	{
		return seg.itemNumber();
	}
	
	public boolean contains(String key)
	{
		return seg.contains(key);
	}

	public CharSequence render()
	{
		return seg.render();
	}

	@Override
	public String toString()
	{
		return render().toString();
	}

	public Segment duplicate()
	{
		SQLSegment ss = new SQLSegment();
		ss.seg = (CharSegment)this.seg.duplicate();
		return ss;
	}

	public void parse(Reader r) throws LoaderException
	{
		seg.parse(r);
	}

	public boolean isEquals(Segment seg)
	{
		return seg.isEquals(seg);
	}

	public String getOriginContent()
	{
		return seg.getOriginContent();
	}

	public void render2(Writer w) throws RenderException
	{
		seg.render2(w);
	}
	
	public SQLSegment fillBy(Object obj)
	{
		SegmentUtils.fillBySegment(this,obj);
		return this;
	}
}
